from urllib import *
import os

class Download2(object):
	"""Class to download and save the file in the disk"""
	localFile = None #The path where the file will be saved
	bytesRead = 0 #total of bytes read in the download function
	totalBytes = 0 #total of bytes of the file
	file = None #stores the file to download
	info = None #stores the header of the file ith informations like 'Content-Type', 'Content-Length' and others
	
	
	def __init__(self, url, localFile):
		"""Initializates the object with the values and get some information about the download"""
		self.url = url
		try:
			self.file = urlopen(self.url)
			self.localFile = localFile
			if not self.file:
				self.file = None
		except IOError:
			self.file = None
	
	def download(self):
		"""Do the download from the url and save in the file defined in localFileName"""
		
		def feedback(transferedBlocks, blockSize, fileTotalSize):
			#os.system('cls')		
			self.bytesRead = (transferedBlocks * blockSize)
			
			self.totalBytes = fileTotalSize		
			
			#print '%d bytes transfered of %d bytes total.' % (self.bytesRead, fileTotalSize)
		
		if self.file:
			urlretrieve(self.file.geturl(), self.localFile, feedback)
			
	
		
			
			
			
			
			
			
			
